package com.ce.pms.renovation.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.common.util.UpLoadFile;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.vo.ImageVO;
import com.ce.pms.renovation.service.ICertificateManageService;
import com.ce.pms.renovation.vo.LicenceListVO;
import com.ce.pms.renovation.vo.LicenceQueryVO;
import com.ce.pms.renovation.vo.PassVO;
import com.ce.pms.renovation.vo.RenovateLicenceVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * 证件管理
 * @author 黄彦军
 *
 */
public class CertificateAction extends PmsBaseAction {

	private ICertificateManageService certificateManageService;

	private ISysrefcodeService sysrefcodeService;

	private IBuildingService buildingService;

	/**
	 * 功能描述: 导航到许可证列表页面(出入证办理分帧页，第一次加载，空页面) <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-22  <br>
	 */

	public String  navigateLicenceEmptyList(){

		setAttribute("totalRows", new Integer(0));
		return "licenceList";
	}

	/**
	 * 功能描述:设置许可证查询条件 <br>
	 * @param queryVO
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-23  <br>
	 */
	public void setLicenceSearchCondition(@Read LicenceQueryVO queryVO){
		setAttribute("licenceSearchCondition", queryVO,Scope.SESSION);
	}

	/**
	 * 功能描述:获取许可证查询条件  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-23  <br>
	 */
	public LicenceQueryVO getLicenceSearchCondition(){
		LicenceQueryVO queryVO = (LicenceQueryVO) getAttribute("licenceSearchCondition");
		return queryVO == null ? new LicenceQueryVO() : queryVO ;
	}

	/**
	 * 功能描述:查询许可证  <br>
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String queryLicence(
			@Read(key = "ec_crd",defaultValue="15")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){

		LicenceQueryVO queryVO = getLicenceSearchCondition();
		queryVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		queryVO.setPageNo(pageNo);
		queryVO.setPageSize(pageSize);

		Page page = certificateManageService.queryLicences(queryVO);

		List<LicenceListVO> licenceList = page.getResult();
		Long totalRows = page.getTotalCount();

		setAttribute("licenceList",licenceList);
		setAttribute("totalRows", totalRows == null ? 0 : totalRows.intValue());
		setAttribute("ec_p", pageNo);

		return "licenceList";
	}

	/**
	 * 功能描述:导航到许可证高级查询页面  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String navigateLicenceSearch(){

		setAttribute("buildingList",  buildingService.getAllBuildingList());
		setAttribute("itemConfigList", sysrefcodeService.querySysrefCodesByCode("RM_RENOVATE_CHARGE_OBJECT"));//收费对象
		setAttribute("feeStateList", sysrefcodeService.querySysrefCodesByCode("OM_CHARGE_STATE"));//收费状态
		setAttribute("licenceStateList", sysrefcodeService.querySysrefCodesByCode("RM_RENOVATE_LICENCE_STATE"));//许可证发放状态

		return "licenceSearch";
	}

	/**
	 * 功能描述: 检查是否可以进行发证 <br>
	 * @param licenceId
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-29  <br>
	 */
	public void checkAddPass(@Read (key="licenceId") Long licenceId){

			RenovateLicenceVO licenceVO = certificateManageService.getLicenceByLicenceId(licenceId);
			Date sysDate = certificateManageService.getDate();
			Date endDate = licenceVO.getEndDate();
			Double day = DateUtil.dateDiff("dd",endDate,sysDate);
			int difference = day.intValue();

			if(!SysrefcodeDO.RM_RENOVATE_LICENCE_STATE_OK.equals(licenceVO.getState())){//许可证未发，不能进行出入证发放
				this.renderText("no");
			}else if(difference > 0){ //当前日期已超过许可证结束日期，提醒
				this.renderText("exceed");
			}else{
				this.renderText("true");
			}

	}

	/**
	 * 功能描述:导航到出入证发放主页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String navigatePassSendMain(@Read (key="licenceId")@Out (key = "licenceId") Long licenceId){

		RenovateLicenceVO licenceVO =certificateManageService.getLicenceByLicenceId(licenceId); //查询许可证信息
		setAttribute("licenceVO", licenceVO);
		return "passSendMain";
	}

	/**
	 * 功能描述:导航到出入证列表页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String navigatePassList(@Read (key="licenceId")@Out(key="licenceId") Long licenceId){

		List<PassVO> passList=certificateManageService.getPasses(licenceId);

		setAttribute("passList", passList);
		setAttribute("totalRows", passList== null ? 0:passList.size());

		return "passList";
	}

	/**
	 * 功能描述:导航到新增出入证页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String navigateAddPass(@Read (key="licenceId") @Out (key = "licenceId") Long licenceId){
		setAttribute("workTypeList", sysrefcodeService.querySysrefCodesByCode("RM_TYPE_OF_WORK"));//工种列表
		PassVO passVO = new PassVO();
		passVO.setPassNumber(certificateManageService.generatePassNumber());
		setAttribute("passVO", passVO);
		return "passAdd";
	}

	/**
	 * 功能描述:增加出入证  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String addPass(@Read PassVO passVO,@Read (key="flush")String flush) throws Throwable{

		UpLoadFile upLoadFile=new UpLoadFile();
		ImageVO imageVO=null;
		try {
			List <ImageVO> list=upLoadFile.parseFormAndUpload(this.getRequest(), passVO);
			for(ImageVO vo:list) {//相片
				if(vo.getFieldName().indexOf("empPhoto")>-1){
					vo.setCreator(getUserId());
					passVO.setPhotoVo(vo);
					imageVO=vo;
				}
			}

			passVO.setCreateId(getUserId());
			certificateManageService.addPass(passVO);
		} catch (Throwable exception) {
			  exception.printStackTrace();
			   if(imageVO != null)
				   upLoadFile.deleteFile(imageVO.getPath(), imageVO.getName());
			   throw exception;
		}

		setAttribute("flush", flush);
		setAttribute("iframeName", "passSendMainIfm");
		return "onlyRefresh";

	}

	/**
	 * 功能描述:检查是否可以修改  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public void checkEditPass(@Read (key="passId") Long passId){

		try {
			certificateManageService.isAllowDeleteOrEditPass(passId);
			this.renderText("true");
		} catch (BusinessException e) {
			this.renderText(super.getLocaleMessage(e.getMessage()));
		}
	}

	/**
	 * 功能描述:导航到修改出入证页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String navigateEditPass(@Read (key="passId")@Out (key = "passId") Long passId){

		PassVO passVO =certificateManageService.getPass(passId);
		setAttribute("workTypeList", sysrefcodeService.querySysrefCodesByCode("RM_TYPE_OF_WORK"));//工种
		setAttribute("passVO", passVO);

		return "passAdd";
	}

	/**
	 * 功能描述:修改  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String editPass(@Read PassVO passVO) throws Throwable{

		UpLoadFile upLoadFile=new UpLoadFile();
		ImageVO imageVO=new ImageVO();;
		try {
			List <ImageVO> list=upLoadFile.parseFormAndUpload(getRequest(), passVO);
			for(ImageVO vo:list) {//相片
				if(vo.getFieldName().indexOf("empPhoto")>-1){
					imageVO=vo;
					imageVO.setCreator(getUserId());
				}
			}

//			if(list != null && list.size() >0){
//				imageVO = list.get(0);
//				imageVO = upLoadFile.wrapImage("empPhoto", imageVO);
//				imageVO.setCreator(getUserId());
//			}

			upLoadFile.wrapImage("empPhoto", imageVO);

			if(imageVO.getName() == null || "".equals(imageVO.getName())){
				imageVO= null;
			}
			passVO.setPhotoVo(imageVO);
			passVO.setCreateId(getUserId());
			certificateManageService.editPass(passVO);
		} catch (Throwable exception) {
			  exception.printStackTrace();
			   if(imageVO != null && imageVO.getId()== null)
				   upLoadFile.deleteFile(imageVO.getPath(), imageVO.getName());
			   throw exception;
		}

		setAttribute("iframeName", "passSendMainIfm");

		return "onlyRefresh";
	}

	/**
	 * 功能描述:检查是否否可以删除  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public void checkDeletePass(@Read(key="passId")Long passId){
		try {
			certificateManageService.isAllowDeleteOrEditPass(passId);
			this.renderText("true");
		} catch (BusinessException e) {
			this.renderText(super.getLocaleMessage(e.getMessage()));
		}
	}

	/**
	 * 功能描述:删除  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String deletePass(@Read(key="passId")Long passId){

		certificateManageService.deletePass(passId);

		setAttribute("iframeName", "passSendMainIfm");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:导航到退证页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-29  <br>
	 */
	public String navigateReturnPass(@Read (key="licenceId")Long licenceId){

		RenovateLicenceVO licenceVO = certificateManageService.getLicenceByLicenceId(licenceId);
		List<PassVO> passList = certificateManageService.getChargePasses(licenceId);

		setAttribute("licenceVO", licenceVO);
		setAttribute("passList", passList);

		return "passReturn";
	}

	/**
	 * 功能描述:出入证退还  <br>
	 * @param passIds
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	public String returnPass(@Read(key="idAndSta")String idAndSta){
		Map<Long, String> idSta= stringToMap(idAndSta);

		certificateManageService.returnPass(idSta);

		setAttribute("iframeName", "refleshLiceneceFrame");
		return "onlyRefresh";
	}

	/**
	 * 功能描述: 导航到出入证详细页面 <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-27  <br>
	 */
	@SuppressWarnings("unchecked")
	public String navigatePassDetail(@Read(key = "licenceId")Long licenceId) {

		RenovateLicenceVO licenceVO = certificateManageService.getLicenceByLicenceId(licenceId); // 查询许可证信息
		List<PassVO> passList = certificateManageService.getPasses(licenceId);

		setAttribute("licenceVO", licenceVO);
		setAttribute("passList", passList);
		setAttribute("totalRows", passList == null ? 0 : passList.size());

		return "passDetail";
	}

	/**
	 *将字符串转成 Map
	 */
	private  Map<Long, String> stringToMap(String infos){
		Map<Long, String> map = new HashMap<Long, String>();
		if (BlankUtil.isBlank(infos))
			return null;
		String [] info = infos.split(";");
		for (int i = 0; i < info.length; i++) {
			String temp= info[i];
			String [] idSta =  temp.split(":");
			map.put(new Long(idSta[0]), idSta[1]);
		}

		return map;

	}

	/************************************************************************************/

	public void setCertificateManageService(
			ICertificateManageService certificateManageService) {
		this.certificateManageService = certificateManageService;
	}


	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}


	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}
}















